﻿using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;

namespace AHCS
{
    public class PdfUtils
    {
        /// <summary>
        /// inputFolder：待拼接⽂件夹
        /// outFile：输出拼接⽂件路径
        /// </summary>
        public static void CombinePDF(string inputFolder, string outFile)
        {
            DirectoryInfo dirScan = new DirectoryInfo(inputFolder);
            FileInfo[] fileinfo = dirScan.GetFiles("*.pdf");
            string[] fileList = new string[fileinfo.Length];
            for (int i = 0; i < fileinfo.Length; i++)
            {
                fileList[i] = fileinfo[i].FullName;
            }
            PdfReader reader;

            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            for (int i = 0; i < fileList.Length; i++)
            {
                reader = new PdfReader(fileList[i]);
                int iPageNum = reader.NumberOfPages;
                for (int j = 1; j <= iPageNum; j++)
                {
                    document.NewPage();
                    newPage = writer.GetImportedPage(reader, j);
                    cb.AddTemplate(newPage, 0, 0);
                }
            }
            document.Close();
        }

        /// <summary>
        /// 合并PDF
        /// </summary>
        /// <param name="fileList"></param>
        /// <param name="outFile"></param>
        public static void CombinePDF(string[] fileList, string outFile)
        {
           
            PdfReader reader;

            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            for (int i = 0; i < fileList.Length; i++)
            {
                reader = new PdfReader(fileList[i]);
                int iPageNum = reader.NumberOfPages;
                for (int j = 1; j <= iPageNum; j++)
                {
                    document.NewPage();
                    newPage = writer.GetImportedPage(reader, j);
                    cb.AddTemplate(newPage, 0, 0);
                }
            }
            document.Close();
        }



        /// <summary>
        /// inputFile：待拆分⽂件路径
        /// outFolder：输出拆分⽂件⽂件夹
        /// </summary>
        protected void SplitPDF(string inputFile, string outFolder)
        {
            PdfReader reader = new PdfReader(inputFile);
            int startPage = 1;
            int endPage = reader.NumberOfPages;
            string splitFileName = string.Empty;
            //按照页码遍历PDF，⼀页拆分⼀个PDF
            for (int i = 1; i <= endPage; i++)
            {
                Document document = new Document(reader.GetPageSizeWithRotation(startPage));
                splitFileName = Path.Combine(outFolder, "page_" + i + "_" + Path.GetFileName(inputFile));
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(splitFileName, FileMode.Create));
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                int rotation;
                while (startPage < i + 1)
                {
                    document.SetPageSize(reader.GetPageSizeWithRotation(startPage));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, startPage);
                    rotation = reader.GetPageRotation(startPage);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(startPage).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    startPage++;
                }
                document.Close();
            }
            reader = null;
          
    } }
}
